﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.ProfessionalTeam.BLL;
using Yipusoft.ProfessionalTeam.DAL;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;

public partial class Combat_ProfessionalTeam_NewProsGrade : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.UrlReferrer != null)
            {
                ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
            }
            BindGrade();
        }
    }

    void BindGrade()
    {
        string DepartmentId = Request["DepartmentId"];
        string IsCadre = Request["IsCadre"];
        string PersonId = Request["PersonId"];
        TeamDataContext tdc = new TeamDataContext();
        lblDepartmentId.Text = Yipusoft.Account.BLL.Department.GetDepartment(DepartmentId).FullName;

        if (IsCadre == "1")
        {
            Yipusoft.MapInfo.Model.SoldierArchiveInfo cadre = new Yipusoft.MapInfo.BLL.Cadre().GetSoldiersByWhere("CadreID='" + PersonId + "'").FirstOrDefault();
            if (cadre != null)
            {
                lblName.Text = cadre.CadreName;
            }
        }
        else
        {
            Yipusoft.SoldierArchives.BLL.Soldier soldier = new Yipusoft.SoldierArchives.BLL.Soldier();
            SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(PersonId);
            if (soldierInfo != null)
            {
                lblName.Text = soldierInfo.SoldierName;
            }
        }
        ProsGrade grade = tdc.ProsGrade.Where(o => o.IsCadre == Convert.ToInt32(IsCadre) && o.personId == new Guid(PersonId)).FirstOrDefault();
        if (grade != null)
        {
            txtFWCGrade.Text = grade.FWCGrade;
            txtFWCSorce.Text = grade.FWCScore.ToString();
            txtYWQZGrade.Text = grade.YWQZGrade;
            txtYWQZSore.Text = grade.YWQZScore.ToString();
            txtDGGrade.Text = grade.DGGrade;
            txtDGSore.Text = grade.DGScore.ToString();
            txtSGGrade.Text = grade.SGGrade;
            txtSGSore.Text = grade.SGScore.ToString();
            txtRun5000Grade.Text = grade.Run5000Grade;
            txtRun5000Sore.Text = grade.Run5000Score.ToString();
            txt5000FGrade.Text = grade.Run5000FGrade;
            txtRun5000FSore.Text = grade.Run5000FScore.ToString();
            txt100FGrade.Text = grade.Run100FGrade;
            txtRunFSore.Text = grade.Run100FScore.ToString();
            txtTheory.Text = grade.TheoryScore.ToString();

        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string DepartmentId = Request["DepartmentId"];
            string IsCadre = Request["IsCadre"];
            string PersonId = Request["PersonId"];
            TeamDataContext tdc = new TeamDataContext();
            ProsGrade grade = tdc.ProsGrade.Where(o => o.IsCadre == Convert.ToInt32(IsCadre) && o.personId == new Guid(PersonId)).FirstOrDefault();
            if (grade != null)
            {//修改
                grade.FWCGrade = txtFWCGrade.Text;
                grade.FWCScore = Convert.ToDecimal(txtFWCSorce.Text);
                grade.YWQZGrade = txtYWQZGrade.Text;
                grade.YWQZScore = Convert.ToDecimal(txtYWQZSore.Text);
                grade.DGGrade = txtDGGrade.Text;
                grade.DGScore = Convert.ToDecimal(txtDGSore.Text);

                grade.SGGrade = txtSGGrade.Text;
                grade.SGScore = Convert.ToDecimal(txtSGSore.Text);

                grade.Run5000Grade = txtRun5000Grade.Text;
                grade.Run5000Score = Convert.ToDecimal(txtRun5000Sore.Text);

                grade.Run5000FGrade = txt5000FGrade.Text;
                grade.Run5000FScore = Convert.ToDecimal(txtRun5000FSore.Text);

                grade.Run100FGrade = txt100FGrade.Text;
                grade.Run100FScore = Convert.ToDecimal(txtRunFSore.Text);

                grade.TheoryScore = Convert.ToDecimal(txtTheory.Text);

                tdc.SubmitChanges();
            }
            else
            {//添加
                grade = new ProsGrade();
                grade.Id = Guid.NewGuid();
                grade.IsCadre = Convert.ToInt32(IsCadre);
                grade.personId = new Guid(PersonId);
                grade.FWCGrade = txtFWCGrade.Text;
                grade.FWCScore = Convert.ToDecimal(txtFWCSorce.Text);
                grade.YWQZGrade = txtYWQZGrade.Text;
                grade.YWQZScore = Convert.ToDecimal(txtYWQZSore.Text);
                grade.DGGrade = txtDGGrade.Text;
                grade.DGScore = Convert.ToDecimal(txtDGSore.Text);

                grade.SGGrade = txtSGGrade.Text;
                grade.SGScore = Convert.ToDecimal(txtSGSore.Text);

                grade.Run5000Grade = txtRun5000Grade.Text;
                grade.Run5000Score = Convert.ToDecimal(txtRun5000Sore.Text);

                grade.Run5000FGrade = txt5000FGrade.Text;
                grade.Run5000FScore = Convert.ToDecimal(txtRun5000FSore.Text);

                grade.Run100FGrade = txt100FGrade.Text;
                grade.Run100FScore = Convert.ToDecimal(txtRunFSore.Text);

                grade.TheoryScore = Convert.ToDecimal(txtTheory.Text);

                tdc.ProsGrade.InsertOnSubmit(grade);
                tdc.SubmitChanges();
            }

            Response.Redirect(ViewState["UrlReferrer"].ToString());
        }
        catch (Exception ex)
        {
            lblPromp.Text = ex.Message;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["UrlReferrer"].ToString());
    }
}
